package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.InformationDao;
import com.example.dao.UsersDao;
import com.example.domain.Books;
import com.example.domain.Borrow;
import com.example.domain.Information;
import com.example.domain.Users;
import com.example.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class InformationService {

    @Autowired
    private InformationDao informationDao;
    @Autowired
    private UsersDao usersDao;

    /*
    根据userId查询个人信息
     */
    public Information selectInformationById(Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        return information;
    }

    /*
    更新个人信息
     */
    public void updateInfor(String name, Integer sexNum, Date birth, Long id){
        String sex = sexNum==0? "女" : "男";
//        QueryWrapper<Information> qw = new QueryWrapper<>();
        Information information = selectById(id);
        information.setName(name);
        information.setSex(sex);
        information.setBirth(birth);
        informationDao.updateById(information);
    }

    public Information selectById(Long id){
        return informationDao.selectById(id);
    }

    /*
    因为充值，跟新balance
     */
    public void recharge(Integer czMoney, Long id){
        Information information = selectById(id);
        Double balance = information.getBalance();
        balance = balance + czMoney;
        information.setBalance(balance);
        informationDao.updateById(information);
    }

    /*
    因为买书，跟新balance（判断余额是否足够买书）
     */
    public boolean updateBalance(Double bookPrice,Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        Double balance = information.getBalance();
        balance = balance-bookPrice;
        information.setBalance(balance);
        informationDao.updateById(information);
        return true;
    }

    /*
    检查余额是否足够买书
     */
    public boolean seelctBalance(Double bookPrice, Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        Double balance = information.getBalance();
        balance = balance-bookPrice;
        if(balance<0){
            return false;
        }
        return true;
    }

    public void addInfor(Users user){
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.eq("username",user.getUsername());
        Users u = usersDao.selectOne(qw);
        Information information = new Information();
        information.setName(user.getUsername());
        information.setUId(u.getId());
        information.setBirth(new Date());
        informationDao.insert(information);
    }

    public void updatePhoto(String photo,Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        information.setPhoto(photo);
        informationDao.updateById(information);
    }

    /*
   分页查询
    */
    //    分页查询以及模糊查询
    public Page<Information> selectUsersByPage(String name, Integer current, Integer size){
        QueryWrapper<Information> qw = new QueryWrapper<>();
//        按照搜索查询，name要是不为空就进行模糊查询
        qw.like(StringUtils.isNotBlank(name),"name",name);
//        分页查询，前提需要用到分页查询的插件，也就是config包下的类
        Page<Information> informationPage = new Page<>();
//        设置当前页
        informationPage.setCurrent(current);
//        设置每页展示的数据
        informationPage.setSize(size);
//        调用mybatis-plus里面封装好的方法
        return informationDao.selectPage(informationPage,qw);
    }

    /*
    扣除个人积分
     */
    public Integer downPoints(Borrow borrow){
        Integer length = DateUtils.daysBetween(new Date(), borrow.getTimeEnd());
        Information information = informationDao.selectById(borrow.getUserId());
        Integer points = information.getPoints();
        points = points - length;
        information.setPoints(points);
        informationDao.updateById(information);
        return length;
    }

    /*
    增加个人积分
     */
    public void upPoints(Borrow borrow){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",borrow.getUserId());
        Information information = informationDao.selectOne(qw);
        Integer points = information.getPoints();
        points = points+1;
        information.setPoints(points);
        informationDao.updateById(information);
    }

    /*
    查看积分
     */
    public boolean selectPoints(Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        if(information.getPoints()>0){
            return true;
        }
        return false;
    }
}
